package com.zaimokuza.cn.controller;

import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.blog.Classification;
import com.zaimokuza.cn.entity.response.ResponseFormat;
import com.zaimokuza.cn.service.ClassificationService;
import com.zaimokuza.cn.util.RandomUtils;
import com.zaimokuza.cn.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author zaimokuza
 */
@Api(tags = "分类相关")
@ResponseFormat
@RestController
@RequestMapping("/classification")
public class ClassificationController {

    @Resource
    private ClassificationService classificationService;

    @ApiOperation(value = "添加分类", notes = "添加分类")
    @PostMapping("/add")
    public void addClassification(@RequestBody @Validated Classification classification) {
        // 分类ID和用户ID
        classification.setId(RandomUtils.get32Uid());
        classification.setUserId(UserUtils.getUserId());
        // 添加分类
        classificationService.addClassification(classification);
    }

    @ApiOperation(value = "更新分类", notes = "更新分类")
    @PutMapping("/update")
    public void updateClassification(@RequestBody @Validated Classification classification) {
        // 用户ID
        classification.setUserId(UserUtils.getUserId());
        // 更新分类
        classificationService.updateClassification(classification);
    }

    @ApiOperation(value = "用户删除分类", notes = "用户根据ID删除分类")
    @DeleteMapping("/delete")
    public void deleteClassificationForUser(@RequestParam @ApiParam("分类ID") String id) {
        classificationService.deleteClassificationForUser(id, UserUtils.getUserId());
    }

    @ApiOperation(value = "管理端删除分类", notes = "管理端根据ID删除分类")
    @DeleteMapping("/manageDelete")
    public void deleteClassificationForManage(@RequestParam @ApiParam("分类ID") String id) {
        classificationService.deleteClassification(id);
    }

    @ApiOperation(value = "查询分类列表", notes = "查询当前用户的分类列表")
    @GetMapping("/list")
    public ArrayList<Classification> getClassificationList() {
        return classificationService.getClassificationListByUserId(UserUtils.getUserId());
    }

    @ApiOperation(value = "根据域名查询分类列表", notes = "根据域名查询当前用户的分类列表")
    @GetMapping("/list/{domain}")
    public ArrayList<Classification> getClassificationListByDomain(@PathVariable @ApiParam("用户域名") String domain) {
        return classificationService.getClassificationListByUserDomain(domain);
    }

    @ApiOperation(value = "查询分页的分类列表", notes = "查询分页的分类列表")
    @GetMapping("/page")
    public PageResult<Classification> getClassificationPage(@RequestParam(required = false) @ApiParam("分类名称") String name,
                                                            @RequestParam(required = false) @ApiParam("用户名称") String userName,
                                                            @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                                            @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        return classificationService.getClassificationPage(name, userName, page, size);
    }

}
